package com.ruoyi.web.controller.question;

import java.util.List;

import com.ruoyi.common.utils.CodeGeneratorUtil;
import com.ruoyi.question.vo.QuestionVO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.question.domain.QuestionInfo;
import com.ruoyi.question.service.IQuestionInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 题目管理Controller
 * 
 * @author amy
 * @date 2025-06-10
 */
@RestController
@RequestMapping("/question/question")
@Tag(name = "【题目管理】管理")
public class QuestionInfoController extends BaseController
{
    @Autowired
    private IQuestionInfoService questionInfoService;

    /**
     * 查询题目管理列表
     */
    @Operation(summary = "查询题目管理列表")
    @PreAuthorize("@ss.hasPermi('question:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuestionInfo questionInfo)
    {
        startPage();
        List<QuestionInfo> list = questionInfoService.selectQuestionInfoList(questionInfo);
        return getDataTable(list);
    }

    /**
     * 导出题目管理列表
     */
    @Operation(summary = "导出题目管理列表")
    @PreAuthorize("@ss.hasPermi('question:question:export')")
    @Log(title = "题目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionInfo questionInfo)
    {
        List<QuestionInfo> list = questionInfoService.selectQuestionInfoList(questionInfo);
        ExcelUtil<QuestionInfo> util = new ExcelUtil<QuestionInfo>(QuestionInfo.class);
        util.exportExcel(response, list, "题目管理数据");
    }

    /**
     * 获取题目管理详细信息
     */
    @Operation(summary = "获取题目管理详细信息")
    @PreAuthorize("@ss.hasPermi('question:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(questionInfoService.selectQuestionInfoById(id));
    }

    /**
     * 新增题目管理
     */
    @Operation(summary = "新增题目管理")
    @PreAuthorize("@ss.hasPermi('question:question:add')")
    @Log(title = "题目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionInfo questionInfo)
    {
        questionInfo.setCreatedBy(getUsername());
        questionInfo.setQuestionCode(CodeGeneratorUtil.generateCode());
        return toAjax(questionInfoService.insertQuestionInfo(questionInfo));
    }

    /**
     * 修改题目管理
     */
    @Operation(summary = "修改题目管理")
    @PreAuthorize("@ss.hasPermi('question:question:edit')")
    @Log(title = "题目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionInfo questionInfo)
    {
        questionInfo.setUpdatedBy(getUsername());
        return toAjax(questionInfoService.updateQuestionInfo(questionInfo));
    }

    /**
     * 删除题目管理
     */
    @Operation(summary = "删除题目管理")
    @PreAuthorize("@ss.hasPermi('question:question:remove')")
    @Log(title = "题目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable( name = "ids" ) Long[] ids) 
    {
        return toAjax(questionInfoService.deleteQuestionInfoByIds(ids));
    }

    /**
     * 查询用户题目列表
     */
    @Operation(summary = "查询用户题目列表")
    @GetMapping("/userList")
    public TableDataInfo userList(QuestionVO questionVO)
    {
        startPage();
        List<QuestionVO> list = questionInfoService.selectUserQuestionList(questionVO);
        return getDataTable(list);
    }
}
